<?php

/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 


/**
 *  @file
 *  @Author Samuel Maura
 *  Login script
 */

require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/debug.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/user.php");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/sqlSafe.php");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/validate.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/lib/session/sessionUtils.inc");

// Create session
session_start();
//Copy post values
savePost();
$_SESSION['validation_error'] = "";
$validatedInputs = array();
$inputs = trimInputArray($_POST);
$inputs['username'] = strtolower($inputs['username']) ;

validateLogin($inputs['username'],$inputs['password']);
checkErrors('validation_error','/xenix/views/wikilogin.php');

// Check again
 if ($_SESSION['validation_error'] == ''){
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/user.php");
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/sqlSafe.php");
    
    $user = new User();
    $sqlSafe = new SqlSafe(dbManager::connect());
    
    //var_dump($validatedInputs);
    
    foreach($validatedInputs as $key => $val){
      $validatedInputs[''.$key.''] = $sqlSafe->safe($val);
    }
    
    if ($user->confirmCredentials($validatedInputs['username'],$validatedInputs['password'] )){
         $_SESSION = array();
         session_regenerate_id(true);
         $_SESSION['AUTHORIZED'] = 1;
         $_SESSION['username'] = $user->getUserName();
         $_SESSION['email'] = $user->getEmail();
         $_SESSION['user_id'] = $user->getUserId();
         header("Location: /xenix/views/wikidashboard.php");
        exit();
    }
    else{
      $_SESSION['validation_error'] = "This username or password is invalid.";
      header("Location: /xenix/views/wikilogin.php");
      exit();
    }
 }





function validateLogin($username,$password){
  $validate = new Validate();
  
  if ( isset($username) && ($username != '') ){
       if (!$validate->validUserName($username)){
         $_SESSION['validation_error'] .= "Username format invalid <br/>";
       }
       else{
          $GLOBALS['validatedInputs']['username'] = $username;   
       }
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "Username is required <br/>";
  }
 
  if ( isset($password) && ($password != '') ){
     $GLOBALS['validatedInputs']['password'] = $password;   
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "password required <br/>";
  }
  
} 



?>